package com.simin.base.apputils;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.UiThread;

import com.simin.base.R;

/**
 * date:2023/10/4
 * author:wsm
 * faucation:toast显示
 */
public class ToastUtils {
    private static Context mContext;
    private TextView tipsText;
    private Toast toast = null;

    @UiThread
    public static void initialize(Context context) {
        mContext = context.getApplicationContext();
    }
    public ToastUtils() {
    }

    public void InitToast() {
        if (toast == null) {
            toast = new Toast(mContext);
            View view = LayoutInflater.from(mContext).inflate(R.layout.toast_hint_layout, null, false);
            tipsText = view.findViewById(R.id.toast_content_tv);
            toast.setView(view);
        }
    }

    public void setGravity(int gravity) {
        toast.setGravity(gravity, 0, 0);
    }

    public void setText(String tips) {
        if (TextUtils.isEmpty(tips)) {
            tipsText.setText("");
            return;
        }
        tipsText.setText(tips);
    }

    public void show() {
        toast.show();
    }

    public void setShowTime(int time) {
        toast.setDuration(time);
    }

    public void setTextColor(int color) {
        tipsText.setTextColor(mContext.getResources().getColor(color));
    }

    public void setTextSize(float size) {
        tipsText.setTextSize(size);
    }

    /**
     * 居中显示提示
     *
     * @param content
     */
    public static void show( String content) {
        ToastUtils format = new ToastUtils();
        format.InitToast();
        format.setText(content);
        format.setGravity(Gravity.CENTER);
        format.show();
    }

    /**
     * 居中显示提示
     *
     * @param content 显示内容
     * @param time    显示时长
     */
    public static void show(String content, int time) {
        ToastUtils format = new ToastUtils();
        format.InitToast();
        format.setText(content);
        format.setShowTime(time);
        format.setGravity(Gravity.CENTER);
        format.show();
    }
}
